package com.socket;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;

public class DASocketServer {
	private static int PORT;
	IServer server;
	SwingChatServer chatServer;
	public static Map<String, INonBlockingConnection> userHash = null;
	public static List<String> msgList = null;
	private ServerDataHandler serverDataHandle = null;

	public DASocketServer(int port, SwingChatServer chatServer)
			throws UnknownHostException, IOException {
		PORT = port;
		this.chatServer = chatServer;

		userHash = Collections
				.synchronizedMap(new WeakHashMap<String, INonBlockingConnection>());
		msgList = Collections.synchronizedList(new LinkedList<String>());

		this.serverDataHandle = new ServerDataHandler();
		if (this.chatServer != null) {
			this.serverDataHandle.setChatServer(this.chatServer);
		}

		this.server = new Server(PORT, this.serverDataHandle);
	}

	public void start() throws UnknownHostException, IOException {
		DAPolicyServerSocket.getInstance().start();
		this.server.start();
	}

	public void close() throws IOException {
		this.server.close();
	}
}
